package com.kate.mall.bff.wx.controller;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.kate.mall.bff.wx.controller.form.SearchMessageForm;
import com.kate.mall.bff.wx.feign.SnmFeignClient;
import com.kate.mall.bff.wx.service.HomeService2;
import com.kate.mall.common.utils.ListMap2ListVoUtils2;
import com.kate.mall.common.utils.R;
import com.kate.mall.common.vo.messsage.MessageRowsVo;
import com.kate.mall.common.vo.messsage.MessageVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 新梦想.陈超
 * @version 2021.2
 * @Description: {TODO}
 * @date 2023/5/31 下午9:09
 */
@RestController
@RequestMapping("wx/message")
public class MessageController {

    @Resource
    private SnmFeignClient snmFeignClient;
    @Resource
    private HomeService2 homeService2;
    @Resource
    private ListMap2ListVoUtils2 listMap2ListVoUtils;

    @GetMapping("refreshMessage")
    public R refreshMessage(){
        Map resultMap = new ConcurrentHashMap();
        R messageR = snmFeignClient.refreshMessage();
        parseFutureRefreshMessage(messageR,resultMap,"messageRows", MessageRowsVo.class);
        return  R.ok().put("data", resultMap);
    }
    @PostMapping("searchMessageByPage")
    public R searchMessageByPage(@RequestBody SearchMessageForm searchMessageForm){
        Map resultMap = new ConcurrentHashMap();
        R messageR = snmFeignClient.searchMessageByPage(searchMessageForm);
         parseFutureMessage(messageR,resultMap,"result", MessageVo.class);

        return  R.ok().put("data", resultMap);
    }
    @PostMapping("searchMessageById")
    public R searchMessageById(@RequestBody SearchMessageForm searchMessageForm){
        Map resultMap = new ConcurrentHashMap();
        R messageR = snmFeignClient.searchMessageById(searchMessageForm);
        MessageVo messageVo = MapUtil.get(messageR, "result", MessageVo.class);
        return  R.ok().put("data", messageVo);
    }
    @PostMapping("updateUnreadMessage")
    public R updateUnreadMessage(@RequestBody SearchMessageForm searchMessageForm){
        Map resultMap = new ConcurrentHashMap();
        R messageR = snmFeignClient.updateMessageById(searchMessageForm);
        Boolean updateResult = MapUtil.get(messageR, "result", Boolean.class);
        return  R.ok().put("data", updateResult);
    }
    private void parseFutureMessage(R messageR, Map resultMap, String result, Class<MessageVo> messageVoClass) {
//        List<Map> listMapMessage = MapUtil.get(messageR,"result",List.class);
//        List<MessageVo> messageVoList = listMap2ListVoUtils.<MessageVo>listMap2ListVo(listMapMessage, messageVoClass);
        List<MessageVo> messageVoList = JSON.parseArray(JSON.toJSONString(MapUtil.get(messageR,"result",List.class)),MessageVo.class);
        resultMap.put("result",messageVoList.size()>0?messageVoList:null);
    }


    public void parseFutureRefreshMessage(R messageR,Map resultMap, String responseKey, Class<MessageRowsVo> voClazz){
        if(messageR==null) return;
        MessageRowsVo messageRowsVo = null;
        try {
            messageRowsVo = voClazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        messageRowsVo.setLastRows((Integer) messageR.get("lastRows"));
        messageRowsVo.setUnreadRows((Integer) messageR.get("unreadRows"));
        resultMap.put(responseKey, messageRowsVo);
    }

}
